// ================================
// JEND.ui v1.4.2
// base on jQuery & JEND
// updated by jiangxiaoyang, 2011-4-2
// ================================
(function($) {
	// ----------------------------
	if (!JEND || !JEND.base || !JEND.util) return;
	// ----------------------------
	// 只能输入整数或小数
	JEND.ui.NumeralInput = function(elm, params) {
		elm.each(function() { this._param = params || $.getRelData($(this)); });
		elm.css('ime-mode', 'disabled');
		elm.bind('keypress', function(e) {
			try {
				if (e.shiftKey) return false;
				var code=e.which||e.keyCode||0;
				if (this._param.allowFloat) {
					return code==8||(e.which==0&&e.keyCode==37)||(e.which==0&&e.keyCode==39)||(code>=48&&code<=57)||code==46;
				} else {
					return code==8||(e.which==0&&e.keyCode==37)||(e.which==0&&e.keyCode==39)||(code>=48&&code<=57)||(e.which==0&&e.keyCode==46);
				}
				// 8:backspace / 37:left / 39:right / 46:del (FF: del为which=0,keyCode=46)
			} catch(err) {}
		});
		elm.bind('paste',function(){ return !clipboardData.getData('text').match(/\D/); });
		elm.bind('dragenter', function(){ return false; });
	};
	// ----------------------------
	// 定时关闭(隐藏)div图层
	JEND.ui.HideByTimer = function(elm, params) {
		elm.each(function() {
			var element = $(this);
			var param = params || $.getRelData(element);
			setTimeout(function() {
				if (param.animate == 'slide') {
					element.slideUp();
				} else {
					element.hide();
				}
			}, param.timer*1000);
		});
	};
	// ----------------------------
	JEND.ui.ElementHover = function(elm, params) {
		elm.each(function() { this._param = params || $.getRelData($(this)); });
		elm.bind('mouseover', function() { $(this).addClass(this._param.className); });
		elm.bind('mouseout', function() { $(this).removeClass(this._param.className); });
	};
	// ----------------------------
	JEND.ui.ChildHoverView = function(elm, params) {
		elm.each(function() { this._param = params || $.getRelData($(this)); });
		elm.bind('mouseover', function() { $(this).find(this._param.child).show(); });
		elm.bind('mouseout', function() { $(this).find(this._param.child).hide(); });
	};
	// ----------------------------
	// 显示输入框默认文本
	JEND.ui.InputDefault = function(elm, params) {
		elm.each(function() {
			var element = $(this);
			this._param = params || $.getRelData(element);
			element.attr('_defval', this._param.value);
			element.attr('_allowempty', this._param.allowempty||'no');
			$(this.form).submit(function(e) {
				if ($.trim(element.val())=='' || $.trim(element.val())==element.attr('_defval')) {
					if (element.attr('_allowempty')=='no') {
						element.focus();
						if(e.preventDefault) e.preventDefault();
						return false;
					} else {
						element.val('');
						return true;
					}
				}
			});
		});
		elm.bind('focus', function() {
			if (this._param.color) $(this).css('color', '');
			if (this._param.className) $(this).removeClass(this._param.className);
			if ($.trim($(this).val())==$(this).attr('_defval')) $(this).val('');
		});
		elm.bind('blur', function() { 
			if ($.trim($(this).val())=='' || $.trim($(this).val())==$(this).attr('_defval')) {
				if (this._param.color) $(this).css('color', this._param.color);
				if (this._param.className) $(this).addClass(this._param.className);
				$(this).val($(this).attr('_defval'));
			}
		});
		elm.blur();
	};
	// ----------------------------
	// 图片加载
	JEND.ui.LoadRealImage = function(elm, params) {
		elm.each(function() {
			var element = $(this);
			var param = params || $.getRelData(element);
			var realSrc = element.attr(param.srcAttr || 'realSrc') || '';
			var errorSrc = param.errorUrl || 'http://pic1.eachnet.com/upload/itempic_188x188.GIF';
			var oW = element.attr('width') || element.width() || param.width || 80;
			var oH = element.attr('height') || element.height() || param.height || 80;
			if (realSrc) {
				var imgObj = new JEND.base.ImageLoader({
					src:realSrc,
					onLoad:function() {
						element.removeAttr(param.srcAttr || 'realSrc');
						element.attr('src', this.element.src);
						var w = this.element.width;
						var h = this.element.height;
						if (w && h) {
							var _w = 0;
							var _h = 0;
							if (w/h>=oW/oH) {
								_w = oW; _h = (h*oW)/w;
								if (w < oW && param.autoScale) {
									_w = w; _h = h;
								}
							} else { 
								_w = (w*oH)/h; _h = oH;
								if (h < oW && param.autoScale) {
									_w = w; _h = h;
								}
							}
							var m_w = parseInt((oW - _w) / 2);
							var m_h = parseInt((oH - _h) / 2);
							element.css({width:_w+'px', height:_h+'px'});
							if (param.autoMargin)  element.css({marginTop:m_h, marginLeft:m_w});
							if (param.autoPadding) element.css('padding', m_h+'px '+m_w+'px');
						}
					},
					onError:function() {
						element.attr('src', errorSrc);
					}
				});
			} else {
				element.attr('src', errorSrc);
			}
		});
	};
	// ----------------------------
	// 图片大小调整
	JEND.ui.ImageResizer = function(elm, params) {
		elm.each(function() {
			var element = $(this);
			var param = params || $.getRelData(element);
			var oW = element.attr('width') || element.width() || param.width || 80;
			var oH = element.attr('height') || element.height() || param.height || 80;
			var imgObj = new Image();
			imgObj.resize = function() {
				var w = this.width;
				var h = this.height;
				if (w && h) {
					var _w = 0;
						var _h = 0;
					if (w/h>=oW/oH) {
						_w = oW; _h = (h*oW)/w;
						if (w < oW && param.autoScale) {
							_w = w; _h = h;
						}
					} else { 
						_w = (w*oH)/h; _h = oH;
						if (h < oW && param.autoScale) {
							_w = w; _h = h;
						}
					}
					var m_w = parseInt((oW - _w) / 2);
					var m_h = parseInt((oH - _h) / 2);
					element.css({width:_w+'px', height:_h+'px'});
					if (param.autoMargin)  element.css({marginTop:m_h, marginLeft:m_w});
					if (param.autoPadding) element.css('padding', m_h+'px '+m_w+'px');
				}
			};
			imgObj.onload = function() {
				this.resize();
			};
			imgObj.src = element.attr('src');
		});
	};
	// ----------------------------
	// ContentSlide，列表内容横向/竖向滚动
	JEND.ui.ContentSlide = function(elm, params) {
		elm.each(function() {
			var element = $(this);
			var param = params || $.getRelData(element);
			new JEND.base.ContentSlide(element, param);
		});
	};
	JEND.base.ContentSlide = function(element, param) {
		this.container = element;
		this.child = param.child || 'li';
		this.childcount = this.container.find(this.child).size();
		this.auto  = param.auto || false;
		this.timer = param.timer || 5000;
		this.direct = param.direct || 'left';
		this.width = param.width || 0;
		this.height = param.height || 0;
		this.unit = param.unit || 1;
		var m_this = this;
		this.moveleft = function() {
			m_this.bindButtonEvent('left', 'unbind');
			m_this.container.animate({marginLeft:-m_this.width*m_this.unit}, 350, function() {
			m_this.container.find(m_this.child+':lt('+m_this.unit+')').appendTo(m_this.container);
				m_this.container.css('marginLeft', 0);
				m_this.bindButtonEvent('left', 'bind');
			});
		};
		this.moveright = function() {
			m_this.bindButtonEvent('right', 'unbind');
			m_this.container.find(m_this.child+':gt('+(m_this.childcount-1-m_this.unit)+')').prependTo(m_this.container);
			m_this.container.css('marginLeft', -m_this.width*m_this.unit);
			m_this.container.animate({marginLeft:0}, 350, function() {
				m_this.bindButtonEvent('right', 'bind');
			});
		};
		this.moveup = function() {
			m_this.bindButtonEvent('top', 'unbind');
			m_this.container.animate({marginTop: -m_this.height*m_this.unit}, 350, function() {
				m_this.container.find(m_this.child+':lt('+m_this.unit+')').appendTo(m_this.container);
				m_this.container.css('marginTop', 0);
				m_this.bindButtonEvent('top', 'bind');
			});
		};
		this.movedown = function() {
			m_this.bindButtonEvent('down', 'unbind');
			m_this.container.find(m_this.child+':gt('+(m_this.childcount-1-m_this.unit)+')').prependTo(m_this.container);
			m_this.container.css('marginTop', -m_this.height*m_this.unit);
			m_this.container.animate({marginTop: 0}, 350, function() {
				m_this.bindButtonEvent('down', 'bind');
			});
		};
		this.bindButtonEvent = function(direct, action) {
			var btn = param[direct];
			if (!btn) return;
			$(btn)[action]('click', this['move'+direct]);
		};
		this.start = function() {
			if (!m_this.auto) return;
			if (m_this.slideTimer) clearInterval(m_this.slideTimer);
			m_this.slideTimer = setInterval(m_this['move'+m_this.direct], m_this.timer);
		};
		this.stop = function() {
			if (m_this.slideTimer) clearInterval(m_this.slideTimer);
		};
		this.init = function() {
			this.start();
			if (param.width) {
				this.container.width(this.childcount * this.width);
				if (param.left) $(param.left).click(this.moveleft).hover(this.stop, this.start);
				if (param.right) $(param.right).click(this.moveright).hover(this.stop, this.start);
			}
			if (param.height) {
				this.container.height(this.childcount * this.height);
				if (param.up) $(param.up).click(this.moveup).hover(this.stop, this.start);
				if (param.down) $(param.down).click(this.movedown).hover(this.stop, this.start);
			}
			this.container.hover(this.stop, this.start);
		};
		this.init();
	};
	// ----------------------------
	JEND.ui.SearchWaiting = function(elm) {
		JEND.util.searchwaiting.init();
		elm.bind('click', function() {					
			if ($(this).hasClass('e-no-waiting')) return false;
			if ($(this).attr('href')=='#') return false;
			if ($(this).attr('target')=='_blank') return true;
			JEND.util.searchwaiting.showLayer();
			return true;
		});
	};
	// ----------------------------
	// 显示/隐藏被遮盖对象（主要用于IE6中的object/iframe/select在有遮盖层时的显示hack)
	JEND.ui.ShowOverElements = function(elm, params) {
		var param = params || $.getRelData(elm);
		$(param.element).each(function() {
			if (this._hide) {
				this.style.visibility = 'visible';
				this._hide = false;
			}
		});
	};
	JEND.ui.HideOverElements = function(elm, params) {
		var param = params || $.getRelData(elm);
		var ox = elm.offset().left;
		var oy = elm.offset().top;
		var ow = elm.width();
		var oh = elm.height();
		$(param.element).each(function() {
			var pos = $(this).offset();
			if (!(ox>(pos.left+$(this).width()) || pos.left>(ox+ow) || pos.top>(oy+oh) || oy>(pos.top+$(this).height()))) {
				this.style.visibility = 'hidden';
				this._hide = true;
			}
		});
	};
	// ----------------------------
	// delayload，内容延迟加载
	JEND.ui.DelayLoad = function(elm, param) {
		elm.find('.'+param.className).each(function() {
			this.callback = param.callback;
			if ($(this).offset().top<$(window).height()+$(document).scrollTop()+param.preloadheight) {
				this.callback();
				$(this).removeClass(param.className);
			}
		});
	};
	// ----------------------------
	JEND.ui.TranslateContent = function(elm, params) {
		if (typeof JEND.ui.Translater == 'undefined') {
			JEND.ui.Translater = new(EachnetTranslateAPI.GoogleTranslater())();
		}
		elm.each(function() {
			var element = $(this);
			var param = params || $.getRelData(element);
			var sourceText = param.name;
			if (element.html().length > 0) return;
			if (param.showloading) {
				element.html('翻译中...');
			}
			JEND.ui.Translater.translate(sourceText, function(transTxt, success) {
				element.html((success)?transTxt:sourceText);
			}, {
				srcLang: (param.lang=='de')?'de':'en',
				descLang: 'zh'
			});
		});
	};
	// ----------------------------
	// 展开/收起
	JEND.ui.ExpandMoreList = function(elm, params) {
		var param = params || $.getRelData(elm);
		elm.each(function() {
			var element = $(this);
			var total = element.find('ul li').length;
			var height = element.find('ul li').height();
			if (total > param.limit) {						
				element.find('ul').css('height', height*param.limit +'px');
				element.find(param.btnElm).text(param.expandText).attr('title',param.expandText).attr('class',param.expandClass);
				element.find(param.btnElm).toggle(function() {
					$(this).parents(param.listElm).find('ul').animate({height: height*total}, 500);
					$(this).text(param.collapseText).attr('title',param.collapseText).attr('class',param.collapseClass);
				}, function() {
					$(this).parents(param.listElm).find("ul").animate({height: height*param.limit}, 500);
					$(this).text(param.expandText).attr("title",param.expandText).attr('class',param.expandClass);
				});
			} else {
				element.find(param.btnParent).remove();
			}
		});
	};
	// ----------------------------
	// Tab或广告图切换
	JEND.ui.ToggleTabItems = function(elm, params) {
		elm.each(function() {
			var element = $(this);
			var param = params || $.getRelData(element);
			new JEND.base.ToggleTabItems(element, param);
		});
	};
	JEND.base.ToggleTabItems = function(element, options) {
		this.container  = element;
		this.childItem  = options.child || 'li';
		this.childType  = options.childType || 'first';
		this.childClass = options.childClass || 'on';
		this.tabItem= options.tab;
		this.tabLength  = element.find(options.tab).length;
		this.tabClass   = options.tabClass || '';
		this.tabAction  = options.tabAction || 'mouseover';
		this.tabTimer   = options.tabTimer || 0;
		this.randomShow = options.randomShow || false;
		var m_this = this;
		this.getIndex = function() {
			if (m_this.randomShow) {
				var index = parseInt(Math.random() * m_this.tabLength);				
				m_this.showTab(index);
				return index;
			}
			for (var i=0; i<m_this.tabLength; i++) {
				if (m_this.container.children(m_this.childItem+':eq('+i+')').hasClass(m_this.childClass)) return i;
			}
			return 0;
		};
		this.showTab = function(idx) {
			if (typeof(idx)!='undefined') {
				m_this.index = idx;
			} else {
				m_this.index ++;
				if (m_this.index >= m_this.tabLength) m_this.index = 0;
			}
			if (m_this.tabClass != '') {
				m_this.container.find(m_this.tabItem).removeClass(m_this.tabClass);		
				m_this.container.find(m_this.tabItem+':eq('+m_this.index+')').addClass(m_this.tabClass);
			}
			if (m_this.childType=='first') {
				m_this.container.children(m_this.childItem).removeClass(m_this.childClass);
				m_this.container.children(m_this.childItem+':eq('+m_this.index+')').addClass(m_this.childClass);
			} else {
				m_this.container.find(m_this.childItem).removeClass(m_this.childClass);
				m_this.container.find(m_this.childItem+':eq('+m_this.index+')').addClass(m_this.childClass);
			}
		};
		this.start = function() {
			if (m_this.tabTimer==0) return;
			if (m_this.timer) clearInterval(m_this.timer);
			m_this.timer = setInterval(function() {	m_this.showTab(); }, m_this.tabTimer);
		};
		this.stop = function() {
			if (m_this.timer) clearInterval(m_this.timer);
		};
		this.init = function() {
			m_this.index = m_this.getIndex() || 0;
			m_this.container.find(m_this.tabItem).each(function(i) {
				$(this).bind(m_this.tabAction, i, function(e) {
					m_this.showTab(e.data);
				});
			});
			if (m_this.tabTimer>0) {
				m_this.container.find(m_this.tabItem).hover(m_this.stop, m_this.start);
			}
			m_this.start();
		};
		this.init();
	};
	// ----------------------------
})(jQuery);